package com.demo.word2html.poi.support.office2007;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.converter.xhtml.internal.styles.CSSStyle;
import org.apache.poi.xwpf.converter.xhtml.internal.styles.CSSStylesDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;

import java.io.IOException;
import java.util.Objects;

/**
 * 过滤掉字体
 *
 * @author fenggang.li
 * @create 2019-09-11 13:23
 */
@Slf4j
public class ChangeFontCSSStylesDocument extends CSSStylesDocument {
    public ChangeFontCSSStylesDocument(XWPFDocument document, boolean ignoreStylesIfUnused, Integer indent) throws XmlException, IOException {
        super(document, ignoreStylesIfUnused, indent);
    }

    @Override
    public CSSStyle createCSSStyle(CTRPr rPr, String className) {
        CTRPr ctrPr = decorateCtrpr(rPr);
        CSSStyle cssStyle = super.createCSSStyle(ctrPr, className);
        return cssStyle;
    }

    /**
     * 使用代理类拦截字体相关的功能
     *
     * @param rPr
     * @return
     */
    private CTRPr decorateCtrpr(CTRPr rPr) {
        if (Objects.isNull(rPr)) {
            return null;
        } else {
            CTRPrProxy proxy = new CTRPrProxy(rPr);
            return proxy;
        }
    }
}
